package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.article.CreateOrUpdateArticleDTO;
import io.github.talelin.latticy.model.ArticleDO;
import io.github.talelin.latticy.service.ArticleService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
* @author generator@lms
* @since 2021-04-21
*/
@RestController
@RequestMapping("/v1/article")
@Validated
@PermissionModule("文章")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("")
    @PermissionMeta("创建文章")
    @GroupRequired
    @Logger(template = "{user.nickname}创建了一篇文章")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateArticleDTO dto) {
        System.out.println(dto);
        dto.setUserAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
        articleService.create(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta("更新文章")
    @GroupRequired
    @Logger(template = "{user.nickname}更新了一篇文章")
    public UpdatedVO update(
            @RequestBody @Validated CreateOrUpdateArticleDTO dto,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        System.out.println(dto);
        System.out.println(id);
        articleService.update(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta("删除文章")
    @GroupRequired
    @Logger(template = "{user.nickname}删除了一篇文章")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        ArticleDO article = articleService.getById(id);
        if (article == null) {
            throw new NotFoundException(90000);
        }
        articleService.getBaseMapper().deleteById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @LoginRequired
    public ArticleDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        ArticleDO article = articleService.getById(id);
        if (article == null) {
            throw new NotFoundException(90000);
        }
        return article;
    }

    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<ArticleDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<ArticleDO> pager = new Page<>(page, count);
        //IPage<ArticleDO> paging = articleService.getBaseMapper().selectPage(pager, new QueryWrapper<ArticleDO>().eq("type",0));
        IPage<ArticleDO> paging = articleService.getBaseMapper().selectPage(pager,null);
        return PageUtil.build(paging);
    }

    @PutMapping("/check/{id}")
    @PermissionMeta("审核文章")
    @GroupRequired
    @Logger(template = "{user.nickname}审核了一篇文章")
    public UpdatedVO check(
            @RequestParam Integer audit,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        System.out.println(audit);
        System.out.println(id);
        articleService.checkArticle(audit, id);
        return new UpdatedVO();
    }
}
